<?php

class StringUtils {

    function normalizeData($data) {
		$data = substr("{$data}00000000000000",0,14);
		return $data;
    }

	function normalizeCode($cod, $len) {
		$tmpcod = "";

		if (strlen($cod) == $len) {
			return $cod;
		}
		
		$codlen = $len - strlen($cod);

		for ($i = 0; $i < $codlen; $i++) {
			$tmpcod .= "0";
		}
		$tmpcod .= $cod;
		
		return $tmpcod;
	}
	
    function today() {
     	$today = getdate();
		
		$date = $today['year'] ;
		$date .= "-" . str_repeat("0", 2 - strlen($today['mon'])) . $today['mon'];
		$date .= "-" . str_repeat("0", 2 - strlen($today['mday'])) . $today['mday'] . " " . "00:00:00";
		
		return $date;    
    }
    
    function year() {
	    $today = getdate();
		
		$date = $today['year'] ;
		return $date;
	}
	
    function now () {
    	$today = getdate();
		
		$date = $today['year'] ;
		$date .= "-" . str_repeat("0", 2 - strlen($today['mon'])) . $today['mon'];
		$date .= "-" . str_repeat("0", 2 - strlen($today['mday'])) . $today['mday'] ;
		$date .= " " . str_repeat("0", 2 - strlen($today['hours'])) . $today['hours'] ;
		$date .= ":" . str_repeat("0", 2 - strlen($today['minutes'])) . $today['minutes'] ;
		$date .= ":" . str_repeat("0", 2 - strlen($today['seconds'])) . $today['seconds'] ;

		return $date;    
    }
	
	
	function stringToDateStringMA ($date,$separator="/") {
        //Access
		
		if ("$date" == "") {
        	return "";
        }

       
        $month = substr($date, 5, 2);
        $day = substr($date, 8, 2);
        $year = substr($date, 0, 4);
         
        return "$day${separator}$month${separator}$year";
    }
	
    function stringToDateString ($date) {
        
		if ("$date" == "") {
        	return "";
        }

        if (!strpos($date, "/") == FALSE) {
        	return $date;
        }
       
        $month = substr($date, 4, 2);
        $day = substr($date, 6, 2);
        $year = substr($date, 0, 4);
        
		$time="";
		
		$h = substr($date, 8, 2);
		$m = substr($date, 10, 2);
		$s = substr($date, 12, 2);
		
		if("$h"!="" && "$m"!="" && "$s"!="")
			$time=" $h:$m:$s"; 
        return "$day/$month/$year{$time}";
    }
    
    function dateStringToString ($date) {
    	
        if ( $date == NULL) {
        	return "";
        }
        
        if (strpos($date, "/") == FALSE) {
        	return $date;
        }
        
        $month = substr($date, 3, 2);
        $day = substr($date, 0, 2);
        $year = substr($date, 6, 4);

		$time="";
		if(strlen($date>8)){
			$h = substr($date, 11, 2);
			$m = substr($date, 14, 2);
			$s = substr($date, 17, 2);
			$time="{$h}{$m}{$s}";
		}

			
			
		return  "$year$month$day$time";
    }
    
    function strToDt ($date) {
    	return stringToDateString($date);
    }
    	
	function getActionPath($action){
		$a = split("/",$action);
		
		$b="";
		
		for ($index = 0; $index < sizeof($a)-1; $index++) {
			if($index==0) 	$b.= $a[$index];
			else			$b.= "/".$a[$index];
			
		}
		return $b;
	}
	
	function getAction($action){
		if ($action=='') return "";
		$a = split("/",$action);
		
		return $a[sizeof($a)-1];
	}

    function stringToDateTimeString ($date) {
        
		
		if ("$date" == "") {
        	return "";
        }

        if (!strpos($date, "/") == FALSE) {
        	return $date;
        }
       
        $month = substr($date, 4, 2);
        $day = substr($date, 6, 2);
        $year = substr($date, 0, 4);
        
        $hour = substr($date, 8, 2);
        $min = substr($date, 10, 2);
        $sec = substr($date, 12, 4);

        return "$day/$month/$year $hour:$min:$sec";
    }

}

?>